package com.tring.ysyn.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.tring.ysyn.entity.SysUser;
import com.tring.ysyn.exception.CustomException;
import com.tring.ysyn.model.dto.user.LoginDTO;
import com.tring.ysyn.service.ISysUserService;
import com.tring.ysyn.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录api
 * @author Tring
 * date 2022-10-23
 */
@RestController
public class LoginController {

    @Resource
    private ISysUserService userService;

//    @PostMapping("admin/login")
//    public Result login2Admin(@RequestBody @Valid LoginDTO dto) {
//        SysUser user = userService.findUserByAccount(dto.getUserName());
//
//        if(user == null){
//            throw new CustomException(500,"帐号不存在");
//        }
//        if(!user.getPassword().equals(SaSecureUtil.md5BySalt(dto.getPassword(),dto.getUserName()))){
//            throw new CustomException(500,"用户名或者密码错误");
//        }
//        if(user.getStatus() == 0){
//            throw new CustomException(500,"账号已被冻结");
//        }
//        StpUtil.login(user.getId(),"admin");
//        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
//        StpUtil.getSession().set("user", user);
//        SysUser currentUser = UserUtils.getCurrentUser();
//        Map<String,Object> map = new HashMap<>();
//        map.put("user", user);
//        map.put("tokenInfo", tokenInfo);
//        return Result.success(map);
//    }

    /**
     * 登录
     * @param dto
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody @Valid LoginDTO dto) {
        SysUser user = userService.findUserByAccount(dto.getUserName());

        if(user == null){
            throw new CustomException(500,"帐号不存在");
        }

        if(!user.getPassword().equals(SaSecureUtil.md5BySalt(dto.getPassword(),dto.getUserName()))){
            throw new CustomException(500,"用户名或者密码错误");
        }

        if(user.getStatus() == 0){
            throw new CustomException(500,"账号已被冻结");
        }
        StpUtil.login(user.getId(),"web");
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        StpUtil.getSession().set("user", user);
        Map<String,Object> map = new HashMap<>();
        map.put("user", user);
        map.put("tokenInfo", tokenInfo);
        return Result.success(map);
    }

    /**
     * 退出当前用户
     * @return
     */
    @GetMapping("logout")
    public Result logout() {
        Object loginId = StpUtil.getLoginId();
        String loginDevice = StpUtil.getLoginDevice();
        StpUtil.logout(loginId,loginDevice);
        return Result.success("登出成功");
    }

    /**
     * 当前用户状态
     * @return
     */
    @GetMapping("checkLoginStatus")
    public Result checkLogin(){
        String loginId = StpUtil.getLoginIdAsString();
        SysUser user = userService.getById(loginId);

        if(user == null){
            throw new CustomException(400,"帐号不存在");
        }
        if(user.getStatus() == 0){
            throw new CustomException(400,"账号已被冻结");
        }
        return Result.success("logined");
    }
}
